//
//  MyBidJobListView.swift
//  GreenDeal
//
//  Created by Moennig Q on 14/12/2022.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit
enum ServiceJobStatusName: String{
    case all = "Job status"
    case started = "Started"
    case unqualified = "Unqualified"
    case finished = "Finished"
    case approved = "Approved"
    case scheduled = "Scheduled"
}
enum ServicePaymentStatusName: String{
    case all = "Payment status"
    case open = "Open"
    case paid = "Paid"
}
class ServiceJobsView: View {

    fileprivate lazy var disposeBag = DisposeBag()
    //MARK: - 跳转到Job status选择页面
    var showJobStatusClosure: (()->Void)?
    //MARK: - 数据list
    var dataObserver: BehaviorRelay<[ServiceJobListModel]> = BehaviorRelay.init(value: [])
    //MARK: - top common view
    let topCommonView = MainTopCommonView()
    //MARK: - 当前筛选的Job状态
    let searchJobStatusLabel = ContentLabel()
    //MARK: - 当前筛选的Job状态
    let searchPaymentStatusLabel = ContentLabel()
    //MARK: - list数据
    let tableView = TableView()
    override func setUpView() {
        self.backgroundColor = UIColor(hexStr: Constant.SystemBackColor)
        //top common view
        topCommonView.showOpertionBtns(showCreateJobBtn: false, showSearchBtn: true)
        self.addSubview(topCommonView)
        topCommonView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        
        //2. table view
        self.addSubview(self.tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(topCommonView.snp.bottom).offset(-70)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
        }
        //1.3 search back view
        let searchBakView = UIView()
        searchBakView.backgroundColor = UIColor.white.withAlphaComponent(0.95)
        searchBakView.layer.cornerRadius = Constant.BackViewCornerRadius
        self.addSubview(searchBakView)
        searchBakView.snp.makeConstraints { (make) in
            make.bottom.equalTo(tableView.snp.top).offset(-25)
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(40)
        }
        //1.3.2 status label
        searchBakView.addSubview(searchJobStatusLabel)
        searchJobStatusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalToSuperview()
            make.right.equalTo(searchBakView.snp.centerX).offset(-32)
            make.bottom.equalToSuperview()
        }
        
        //verticalLineView
        let verticalLineView = View(hexStr: "#D9D9D9FF")
        searchBakView.addSubview(verticalLineView)
        verticalLineView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.height.equalTo(14)
            make.width.equalTo(1)
        }
        let jobStatusDownArrowImageView = UIImageView(image: R.image.installation_list_search_down_arrow())
        searchBakView.addSubview(jobStatusDownArrowImageView)
        jobStatusDownArrowImageView.snp.makeConstraints { (make) in
            make.right.equalTo(verticalLineView.snp.left).offset(-16)
            make.width.height.equalTo(16)
            make.centerY.equalTo(searchBakView)
        }
        //
        searchBakView.addSubview(searchPaymentStatusLabel)
        searchPaymentStatusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(verticalLineView.snp.right).offset(16)
            make.top.equalToSuperview()
            make.right.equalToSuperview().offset(0)
            make.bottom.equalToSuperview()
        }
        //1.3.1 down arrow icon
        let paymentStatusDownArrowImageView = UIImageView(image: R.image.installation_list_search_down_arrow())
        searchBakView.addSubview(paymentStatusDownArrowImageView)
        paymentStatusDownArrowImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-16)
            make.width.height.equalTo(16)
            make.centerY.equalTo(searchBakView)
        }
        self.tableView.drawCorner(byRoundingCorners: [.topLeft,.topRight],backColorHexStr: Constant.TransparentColorHexStr)
        self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView.showsVerticalScrollIndicator = false
        self.tableView.keyboardDismissMode = .onDrag
        //防止上拉加载时数据滚动
        self.tableView.estimatedRowHeight = 200
        self.tableView.rowHeight = UITableView.automaticDimension
        //注册cell
        tableView.register(ServiceJobsTableViewCell.self, forCellReuseIdentifier: ServiceJobsTableViewCell.reuseCellIdentifier)
        //设置cell信息，绑定数据源
        dataObserver.bind(to: tableView.rx.items(cellIdentifier: ServiceJobsTableViewCell.reuseCellIdentifier,cellType: ServiceJobsTableViewCell.self)){(row, model, cell) in
            cell.setData(model)
        }.disposed(by: self.disposeBag)
        //观察数据变化，刷新数据
        dataObserver.asObservable().subscribe(onNext: {[weak self] (list) in
            self?.tableView.reloadData()
        }).disposed(by: disposeBag)
        //
    }
}
